package core
{
	import flash.events.EventDispatcher;
	import flash.utils.Dictionary;
	
	import interfaces.IHookListener;
	import interfaces.IMsgListener;

	public class MsgManager extends EventDispatcher
	{		
		private var m_msgFilter:Vector.<IHookListener>;
		
		public function MsgManager()
		{
			m_msgFilter = new Vector.<IHookListener>();
		}
		
		// 添加监听
		public function addListener(msg:String,msgListener:IMsgListener):void
		{
			addEventListener(msg,msgListener.msgManager);
		}
		
		// 删除监听
		public function removeListener(msg:String,msgListener:IMsgListener):void
		{
			removeEventListener(msg,msgListener.msgManager);
		}
		
		// 派发事件
		public function dispatchMsg(msg:String,data:Object = null):void
		{
			for(var i:int = 0; i < m_msgFilter.length; i++)
			{
				if(m_msgFilter[i].hookMsg(msg,data))return;
			}
			dispatchEvent(new DataEvent(msg,data));
		}
		
		// 挂起事件
		public function hookMsg(ihl:IHookListener):void
		{
			var id:int = m_msgFilter.indexOf(ihl);
			if(id < 0)
				m_msgFilter.push(ihl);
		}
		
		// 取消挂起事件
		public function unhookMsg(ihl:IHookListener):void
		{
			var id:int = m_msgFilter.indexOf(ihl);
			if(id >= 0)
				m_msgFilter.splice(id,1);
		}
	}
}